<?php
/* @var $this ProjectController */
/* @var $model Exam */
/* @var $form CActiveForm */
?>
<script type="text/javascript">
$(function() {
	$( "#exam-form" ).submit(function( event ) {
		//alert($('[name="Exam[examResult]"]').is(':checked'));
		if (($('#Exam_resultDate').val().trim()=="")){
			//$('.errorForm').html('<br><br><div align="center">กรุณากรอกข้อมูลวันประกาศผลสอบ</div>');
        	//$('#dialogError').dialog("open");
        	$('#Exam_resultDate').focus();
		  event.preventDefault();
		}else if($('#Exam_possible').val().trim()==""||$('#Exam_possibleless').val().trim()==""||((eval($('#Exam_possible').val())+eval($('#Exam_possibleless').val()))!=3)){
			$('.errorForm').html('<br><br><div align="center">กรอกข้อมูลความเห็นไม่ถูกต้อง</div>');
        	$('#dialogError').dialog("open");
			$('#Exam_possible').focus();
			event.preventDefault();
		}else if(!$('[name="Exam[examResult]"]').is(':checked')){
			$('.errorForm').html('<br><br><div align="center">กรุณาเลือกผลการสอบ</div>');
        	$('#dialogError').dialog("open");
			$('#Exam_examResult').focus();
			event.preventDefault();
		}else{
			return;
			}
		});
	$('#Exam_possible').blur(function(){
		if ($.isNumeric($('#Exam_possible').val())){
		if ($('#Exam_possible').val()>=0&&$('#Exam_possible').val()<=3) {
			$('#Exam_possibleless').val(3-$('#Exam_possible').val());
			//$('#cname3').focus();
			}else{
				$('#Exam_possible').val("");
				$('.errorForm').html('<br><br><div align="center">กรอกจำนวนอความเห็นได้ไม่เกิน 3 ท่าน</div>');
            	$('#dialogError').dialog("open");
				}
		}else{
			$('#Exam_possible').val("");
			$('.errorForm').html('<br><br><div align="center">กรอกเป็นตัวเลขเท่านั้น</div>');
        	$('#dialogError').dialog("open");
			}
	});
	$('#Exam_possibleless').blur(function(){
		if ($.isNumeric($('#Exam_possible').val())){
		if ($('#Exam_possibleless').val()>=0&&$('#Exam_possibleless').val()<=3) {
			$('#Exam_possible').val(3-$('#Exam_possibleless').val());
			//$('#cname3').focus();
			}else{
				$('#Exam_possibleless').val("");
				$('.errorForm').html('<br><br><div align="center">กรอกจำนวนอความเห็นได้ไม่เกิน 3 ท่าน</div>');
            	$('#dialogError').dialog("open");
				}
		}else{
			$('#Exam_possibleless').val("");
			$('.errorForm').html('<br><br><div align="center">กรอกเป็นตัวเลขเทานั้น</div>');
        	$('#dialogError').dialog("open");
			}
	});
	$("#Exam_criteria").click(function(){
		if (this.checked)
		{
			if ($("#Exam_examResult:checked").val()=='Y'){
			$("#description").show();
			}else{
				$("#description").hide();
				event.preventDefault();
				return false;
			}
		}else{
			$("#description").hide();
		}
	});
	$('[name="Exam[examResult]"]').click(function(){
		if ($("#Exam_criteria").is(":checked")&&$('[name="Exam[examResult]"]:checked').val()=='Y')
		{
			$("#description").show();
		}else{
			$("#description").hide();
			$("#Exam_criteria").prop('checked',false);
		}
	});
<?php if(!$model->criteria=='Y'){?>
	$("#description").hide();
	<?php }?>
});
</script>
<div class="form">

<?php

$form = $this->beginWidget ( 'CActiveForm', array (
		'id' => 'exam-form',
		// Please note: When you enable ajax validation, make sure the corresponding
		// controller action is handling ajax validation correctly.
		// There is a call to performAjaxValidation() commented in generated controller code.
		// See class documentation of CActiveForm for details on this.
		'enableAjaxValidation' => true 
) );
?>


	<?php echo $form->errorSummary($model); ?>
<div class="row">
		<b>วันที่ประกาศผล</b> <font color="red">*</font> <br />
		<?php
		$this->widget ( 'zii.widgets.jui.CJuiDatePicker', array (
				'model' => $model,
				'attribute' => 'resultDate',
				'language' => 'th',
				// additional javascript options for the date picker plugin
				'options' => array (
						// 'mode'=>'focus',
						// 'dateFormat'=>'d MM, yy',
						// 'showOn'=>'button', // 'focus', 'button', 'both'
						'dateFormat' => 'dd/mm/yy',
						// 'value'=>strftime('%d/%m/%Y', strtotime($model->docDate)),
						'showAnim' => 'fold',
						'changeMonth' => true,
						'changeYear' => true,
						'beforeShow' => 'js:function(){
	if($(this).val()!=""){
		var arrayDate=$(this).val().split("/");
		arrayDate[2]=parseInt(arrayDate[2])-543;
		$(this).val(arrayDate[0]+"/"+arrayDate[1]+"/"+arrayDate[2]);
	}
	setTimeout(function(){
		$.each($(".ui-datepicker-year option"),function(j,k){
			var textYear=parseInt($(".ui-datepicker-year option").eq(j).val())+543;
			$(".ui-datepicker-year option").eq(j).text(textYear);
		});
	},50);

}',
						'onChangeMonthYear' => 'js:function(){
	setTimeout(function(){
		$.each($(".ui-datepicker-year option"),function(j,k){
			var textYear=parseInt($(".ui-datepicker-year option").eq(j).val())+543;
			$(".ui-datepicker-year option").eq(j).text(textYear);
		});
	},50);
}',
						'onClose' => 'js:function(){
	if($(this).val()!="" && $(this).val()==dateBefore){
		var arrayDate=dateBefore.split("/");
		arrayDate[2]=parseInt(arrayDate[2])+543;
		$(this).val(arrayDate[0]+"/"+arrayDate[1]+"/"+arrayDate[2]);
	}
}',
						'onSelect' => 'js:function(dateText, inst){
	dateBefore=$(this).val();
	var arrayDate=dateText.split("/");
	arrayDate[2]=parseInt(arrayDate[2])+543;
	$(this).val(arrayDate[0]+"/"+arrayDate[1]+"/"+arrayDate[2]);
}' 
				)
				,
				'htmlOptions' => array (
						'style' => 'height:20px;' 
				)
				 
		) );
		?>
		<?php echo $form->error($model,'resultDate'); ?>
	</div>
	<div class="row">
		<b>ให้ความเห็นว่าเป็นไปได้</b> <font color="red">*</font> <br />
		<?php echo $form->textField($model,'possible',array('size'=>4)); ?> ท่าน
		<?php echo $form->error($model,'possible'); ?>
	</div>

	<div class="row">
		<b>ให้ความเห็นว่าเป็นไปได้น้อย</b> <font color="red">*</font> <br />
		<?php echo $form->textField($model,'possibleless',array('size'=>4)); ?> ท่าน
		<?php echo $form->error($model,'possibleless'); ?>
	</div>

	<div class="row">
		<b>ผลการสอบ</b> <font color="red">*</font> <br />

		<?php echo $form->radioButton($model,'examResult',array("value"=>'Y','uncheckValue'=>null)); ?> ผ่าน  <?php echo $form->checkBox($model,'criteria',array("value"=>'Y')); ?> เงื่อนไข<br />
		<?php echo $form->radioButton($model,'examResult',array("value"=>'N','uncheckValue'=>null)); ?> ไม่ผ่าน
		<?php echo $form->error($model,'examResult'); ?>
	</div>

	<div class="row">
		<div id="description">
		<?php echo $form->labelEx($model,'description'); ?>
		<?php echo $form->textArea($model,'description',array('rows'=>6, 'cols'=>50)); ?>
		<?php echo $form->error($model,'description'); ?>
		</div>
	</div>

	<div class="row buttons">
		<?php 
	$this->widget('zii.widgets.jui.CJuiButton',array(
			'buttonType'=>'submit',
			'name'=>'btnSubmit',
			'value'=>'1',
			'caption'=>(!$model->resultDate) ? 'บันทึกข้อมูล' : 'แก้ไขข้อมูล',
			'htmlOptions'=>array('class'=>'ui-button-primary'),
	)
	);
	?>
	</div>

<?php $this->endWidget(); ?>

</div>
<!-- form -->
<?php $this->beginWidget('zii.widgets.jui.CJuiDialog', array( // the dialog
    'id'=>'dialogError',
    'options'=>array(
        'title'=>'เกิดข้อผิดลาพ',
        'autoOpen'=>false,
        'modal'=>true,
        'width'=>350,
        'height'=>270,
'buttons' => array(
		array('text'=>'ปิด','click'=> 'js:function(){$(this).dialog("close");}'),
),
    ),
));?>
<div class="errorForm"></div>
 
<?php $this->endWidget();?>